package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.wbao.dianniu.logical.SharedPreferencesUtils;
import com.wbao.dianniu.R;
import com.zhy.view.DepthPageTransformer;
import com.zhy.view.ViewPagerCompat;
import com.zhy.view.ViewPagerCompat.OnPageChangeListener;

/**
 * 引导页
 * 
 * @author yejianjun
 *
 */
public class IntroActivity extends Activity implements OnClickListener {

	private ViewPagerCompat mViewPager;
	private int[] mImgIds = new int[] { R.drawable.guide_image1,
			R.drawable.guide_image2, R.drawable.guide_image3 };
	private List<ImageView> mImageViews = new ArrayList<ImageView>();
	private Button intoBtn;
	private ImageView tips01,tips02,tips03;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		SharedPreferencesUtils.init(this);
		boolean status = SharedPreferencesUtils.getIsFirstOpen(this);
		if(status){
			setContentView(R.layout.activity_guide);
			initData();
			
			intoBtn = (Button) findViewById(R.id.gointo_app_bt);
			intoBtn.setOnClickListener(this);
			mViewPager = (ViewPagerCompat) findViewById(R.id.id_viewpager);
			mViewPager.setPageTransformer(true, new DepthPageTransformer());
//			mViewPager.setPageTransformer(true, new RotateDownPageTransformer());
			mViewPager.setAdapter(new PagerAdapter()
			{
				@Override
				public Object instantiateItem(ViewGroup container, int position)
				{
					container.addView(mImageViews.get(position));
					return mImageViews.get(position);
				}
				@Override
				public void destroyItem(ViewGroup container, int position,
						Object object)
				{
					container.removeView(mImageViews.get(position));
				}
				@Override
				public boolean isViewFromObject(View view, Object object)
				{
					return view == object;
				}
				@Override
				public int getCount()
				{
					return mImgIds.length;
				}
			});
			mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
				
				@Override
				public void onPageSelected(int position) {
					// TODO Auto-generated method stub
					System.out.println(" position: " + position);
					tipShow(position);
					if(position == (mImgIds.length -1)){
						intoBtn.setVisibility(View.VISIBLE);
					}else{
						intoBtn.setVisibility(View.GONE);
					}
				}
				
				@Override
				public void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) {
				}
				@Override
				public void onPageScrollStateChanged(int state) {
				}
			});
		}else{
			startApp();
		}
	}

	private void startApp() {
		Intent intent = new Intent(this, SplashActivity.class);
		startActivity(intent);
		finish();
	}
	
	private void initData()
	{
		for (int imgId : mImgIds)
		{
			ImageView imageView = new ImageView(getApplicationContext());
			imageView.setScaleType(ScaleType.CENTER_CROP);
			imageView.setImageResource(imgId);
			mImageViews.add(imageView);
		}
		tips01 = (ImageView) findViewById(R.id.tips_01);
		tips02 = (ImageView) findViewById(R.id.tips_02);
		tips03 = (ImageView) findViewById(R.id.tips_03);
	}
	
	private void tipShow(int position){
		if(0 == position){
			tips01.setBackgroundResource(R.drawable.ic_focus_);
			tips02.setBackgroundResource(R.drawable.ic_normal);
			tips03.setBackgroundResource(R.drawable.ic_normal);
		}else if(1 == position){
			tips01.setBackgroundResource(R.drawable.ic_normal);
			tips02.setBackgroundResource(R.drawable.ic_focus_);
			tips03.setBackgroundResource(R.drawable.ic_normal);
		}else if(2 == position){
			tips01.setBackgroundResource(R.drawable.ic_normal);
			tips02.setBackgroundResource(R.drawable.ic_normal);
			tips03.setBackgroundResource(R.drawable.ic_focus_);
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.gointo_app_bt:
			SharedPreferencesUtils.setIsFirstOpen(false);
			startApp();
			break;
		default:
			break;
		}
	}

}
